package com.zut.hospital.service.diagnose;

import com.alibaba.fastjson.JSON;
import com.zut.hospital.dao.diagnose.PatientMapper;
import com.zut.hospital.dao.staff.OfficeMapper;
import com.zut.hospital.dao.staff.UserMapper;
import com.zut.hospital.entity.diagnose.Patient;
import com.zut.hospital.entity.diagnose.PatientExample;
import com.zut.hospital.entity.staff.Office;
import com.zut.hospital.entity.staff.OfficeExample;
import com.zut.hospital.entity.staff.User;
import com.zut.hospital.entity.staff.UserExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

//import com.zut.hospital.dao.OfficeMapper;

/**
 * @author 侯捷
 * @package_name com.zut.hostest.service
 * @date 2019/5/17 15:06
 * @description
 */
@Service
public class PatientServiceImpl implements PatientService {

    @Autowired
    PatientMapper patientMapper;
    @Autowired
    OfficeMapper officeMapper;
    @Autowired
    UserMapper userMapper;

    /*
    获取系统时间并且转化为与数据库一样的格式
    */
    @Override
    public Date getTimeToSql(){
        Date time=new java.sql.Date(new Date().getTime());
        return time;
    }

    @Override
    public String getTime1(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String date2 = format.format(date);
        return date2;
    }

    /*
        获取系统时间并且拼接成字符串
         */
    @Override
    public  String getTime(){
        Date time=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyyMMdd");
        return format.format(time);
    }

    @Override
    public List<Patient> getAllPatient() {
        PatientExample patientExample=new PatientExample();
        List<Patient> list=patientMapper.selectByExample(patientExample);
        return list;
    }

    /*
    通过时间和id查询某科室在某一天看病的人数
     */
    @Override
    public int countByTimeAndOfcId(String time,Integer ofcId){
        int count=0;
        PatientExample patientExample=new PatientExample();
       // OfficeExample officeExample=new OfficeExample();
        PatientExample.Criteria criteria=patientExample.createCriteria();
        criteria.andPatRemarkEqualTo(time);
        List<Patient> list=patientMapper.selectByExample(patientExample);
        for (Patient patient1 : list) {
            if(patient1.getPatOfcId()==ofcId)
                count++;
        }
        return count;
    }

    /*
    新添加一个病人
     */
    @Override
    public int insertOnePatient(Patient patient){
        return patientMapper.insertSelective(patient);
    }
/*
通过查询条件查询病人
姓名，手机号，挂号号码
 */
@Override
public String selectOnePatientByStatus1(Integer status){
    PatientExample patientExample=new PatientExample();
    PatientExample.Criteria criteria=patientExample.createCriteria();
    criteria.andPatStatusEqualTo(status);

    List<Patient> list=patientMapper.selectByExample(patientExample);
    Map<String,String> patientMap;
    List<Map<String,String>> patientList=new ArrayList<>();
    for (Patient patient : list) {
        patientMap=new HashMap<>();
        patientMap.put("patName",patient.getPatName());
        if(patient.getPatSex()==1)
        patientMap.put("patSex","男");//性别需要判断
        else
            patientMap.put("patSex","女");
        //integer可直接.toString();
        patientMap.put("patAge",patient.getPatAge().toString());
        patientMap.put("patConven",patient.getPatConven());
        patientMap.put("patNation",patient.getPatNation());
        patientMap.put("patContact",patient.getPatContact());
        patientMap.put("pautCreateTime",patient.getPatCreateTime().toString());
        patientMap.put("patOfcId",patient.getPatOfcId().toString());
        patientMap.put("patStatus",patient.getPatStatus().toString());
        patientMap.put("patRemark",patient.getPatRemark());
        patientList.add(patientMap);
    }
    return JSON.toJSONString(patientList);
}
    @Override
    public List<Patient> selectOnePatientByStatus(Integer status){
        PatientExample patientExample=new PatientExample();
        PatientExample.Criteria criteria=patientExample.createCriteria();
        criteria.andPatStatusEqualTo(status);
        List<Patient> list=patientMapper.selectByExample(patientExample);
        return list;
    }
    /*
    更改病人的状态码
     */
    @Override
    public int updateStatus(Patient patient,Integer id){
        PatientExample patientExample=new PatientExample();
        PatientExample.Criteria criteria=patientExample.createCriteria();
        criteria.andIdEqualTo(id);
        return patientMapper.updateByExampleSelective(patient,patientExample);
    }
    /*
    通过状态码查询病人个数
     */
    @Override
    public long countByStatus(Integer status){
        PatientExample patientExample=new PatientExample();
        PatientExample.Criteria criteria=patientExample.createCriteria();
        criteria.andPatStatusEqualTo(status);
        return patientMapper.countByExample(patientExample);
    }

    @Override
    public long countBetweenTime(Date time1,Date time2){
        PatientExample patientExample=new PatientExample();
        PatientExample.Criteria criteria=patientExample.createCriteria();
        criteria.andPatCreateTimeBetween(time1,time2);
//        criteria.andPatCreateTimeEqualTo(time2);
        List<Patient> list=patientMapper.selectByExample(patientExample);
        for (Patient patient : list) {
            System.out.println(patient.getPatCreateTime()+"****1");
        }
        return patientMapper.countByExample(patientExample);
    }

    @Override
    public String getAllOffice(){
        OfficeExample officeExample=new OfficeExample();
        OfficeExample.Criteria criteria=officeExample.createCriteria();
        List<Office> list=officeMapper.selectByExample(officeExample);
        Map<String,String> officeMap;
        List<Map<String,String>> officeList=new ArrayList<>();
        for (Office office:list) {
            officeMap=new HashMap<>();
            officeMap.put("officeId",office.getId().toString());
            officeMap.put("officeName",office.getOfcName());
            officeList.add(officeMap);
        }
        return JSON.toJSONString(officeList);
    }
//获取刚添加病人的id
    @Override
    public int getNewPatientId(){
         List<Patient> list=getAllPatient();
        System.out.println(list.get(list.size() - 1).getId());
        return list.get(list.size()-1).getId();

    }
//查询刚添加的病人的部分信息
    @Override
    public String getPatientById(Integer id){
        PatientExample patientExample=new PatientExample();
        PatientExample.Criteria criteria=patientExample.createCriteria();
        criteria.andIdEqualTo(id);
        List<Patient> list=patientMapper.selectByExample(patientExample);

        Map<String,String> patientMap;
        List<Map<String,String>> petientList=new ArrayList<>();
        for (Patient patient : list) {
            patientMap=new HashMap<>();
            patientMap.put("id",patient.getId().toString());
            patientMap.put("patName",patient.getPatName());
            if(patient.getPatSex()==1)
                patientMap.put("patSex","男");
            else
                patientMap.put("patSex","女");
            if(patient.getPatStatus()==1)
                patientMap.put("patStatus","未就诊");
            else if(patient.getPatStatus()==2)
                patientMap.put("patStatus","未付款");
            else
                patientMap.put("patStatus","已完成");
            OfficeExample officeExample=new OfficeExample();
            OfficeExample.Criteria criteria1=officeExample.createCriteria();
            criteria1.andIdEqualTo( patient.getPatOfcId());
            System.out.println(patient.getPatOfcId());
            List<Office> offices=officeMapper.selectByExample(officeExample);
            System.out.println(offices);
            patientMap.put("patOfcName",offices.get(0).getOfcName());
            String newContact=patient.getPatContact().substring(0,3)+"****"+
                    patient.getPatContact().substring(7);
            patientMap.put("patContact",newContact);
            patientMap.put("patConven",patient.getPatConven());
            patientMap.put("patAge",patient.getPatAge().toString());
            String creatime=new SimpleDateFormat("yyyy-MM-dd").format( patient.getPatCreateTime());
            patientMap.put("patCreateTime",creatime);
            patientMap.put("patRemark",patient.getPatRemark());
            petientList.add(patientMap);
        }
         return JSON.toJSONString(petientList);
    }

    @Override
    public int getOfficeIdByConven(String conven){
        PatientExample patientExample=new PatientExample();
        PatientExample.Criteria criteria=patientExample.createCriteria();
        criteria.andPatConvenEqualTo(conven);
        List<Patient> list=patientMapper.selectByExample(patientExample);
        return list.get(0).getPatOfcId();
    }
    @Override
    public int getPatientIdByConven(String conven){
        PatientExample patientExample=new PatientExample();
        PatientExample.Criteria criteria=patientExample.createCriteria();
        criteria.andPatConvenEqualTo(conven);
        List<Patient> list=patientMapper.selectByExample(patientExample);
        return list.get(0).getId();
    }
    @Override
    public String getUserByOfcId(Integer id){
        UserExample userExample=new UserExample();
        UserExample.Criteria criteria=userExample.createCriteria();
        criteria.andOfcIdEqualTo(id);
        List<User> users=userMapper.selectByExample(userExample);

        Map<String,String> userMap;
        List<Map<String,String>> userList=new ArrayList<>();
        for (User user : users) {
            userMap=new HashMap<>();
            userMap.put("adName",user.getAdName());
            userMap.put("userId",user.getId().toString());
            userList.add(userMap);
        }
        return JSON.toJSONString(userList);
    }

    public Map map(Patient patient){
        Map<String,String> patientMap=new HashMap<>();
        patientMap.put("patName",patient.getPatName());
        if(patient.getPatSex()==1)
            patientMap.put("patSex","男");
        else
            patientMap.put("patSex","女");
        if(patient.getPatStatus()==1)
            patientMap.put("patStatus","未就诊");
        else if(patient.getPatStatus()==2)
            patientMap.put("patStatus","未付款");
        else
            patientMap.put("patStatus","已完成");
        OfficeExample officeExample=new OfficeExample();
        OfficeExample.Criteria criteria1=officeExample.createCriteria();
        criteria1.andIdEqualTo( patient.getPatOfcId());
        System.out.println(patient.getPatOfcId());
        List<Office> offices=officeMapper.selectByExample(officeExample);
        System.out.println(offices);
//        patientMap.put("patOfcName",offices.get(0).getOfcName());
//        String newContact=patient.getPatContact().substring(0,3)+"****"+
//                patient.getPatContact().substring(7);
//        patientMap.put("patContact",newContact);
        patientMap.put("patConven",patient.getPatConven());
        patientMap.put("patAge",patient.getPatAge().toString());
        String creatime=new SimpleDateFormat("yyyy-MM-dd").format( patient.getPatCreateTime());
//        patientMap.put("patCreateTime",creatime);
        patientMap.put("patRemark",patient.getPatRemark());
        return patientMap;
    }

    @Override
    public String getPatientByOfficeId(Integer id,Integer currPage,Integer pageSize){
        PatientExample patientExample=new PatientExample();
        PatientExample.Criteria criteria=patientExample.createCriteria();
        criteria.andPatOfcIdEqualTo(id);


        List<Patient> list=patientMapper.selectByExample(patientExample);
        int size=list.size();

        List<Map<String, String>> resultList = new ArrayList<>();

        int firstIndex = (currPage - 1) * pageSize;
        int lastIndex = currPage * pageSize;
        if (currPage * pageSize > size) {
            lastIndex = size;
        }
        for (Patient patient : list.subList(firstIndex,lastIndex)) {
            resultList.add(map(patient));
        }
        return JSON.toJSONString(resultList);
    }

//    @Override
//    public int getOfcIdBySession(HttpSession session) {
//        String name=session.getAttribute("adminUser").toString();
//        UserExample userExample=new UserExample();
//        UserExample.Criteria criteria=userExample.createCriteria();
//        criteria.andAdNameEqualTo(name);
//        List<User> list= userMapper.selectByExample(userExample);
//        return list.get(0).getOfcId();
//    }

    @Override
    public String getPatientByOfcIdAndStatus(Integer ofcId,Integer status) {

        return null;
    }
    @Override
    public int update(int id, Patient patient) {
        PatientExample patientExample=new PatientExample();
//        patient=new Patient();
        PatientExample.Criteria criteria=patientExample.createCriteria();
        criteria.andIdEqualTo(id);
//        if(sex=="男")
//            patient.setPatSex(1);
//        else
//            patient.setPatSex(0);
//        patient.setPatName(name);
//        patient.setPatAge(age);
//        patient.setPatContact(contact);
//        patient.setPatRemark(remark);
//        Map<String,String> map=new HashMap<>();
//        if(patientMapper.updateByExampleSelective(patient,patientExample)==1)
//            map.put("result","修改成功");
//        else
//            map.put("result","修改失败");
//        return JSON.toJSONString(map);
        return  patientMapper.updateByExampleSelective(patient,patientExample);
    }

    @Override
    public int deleteOneInfo(Integer id) {
        PatientExample patientExample=new PatientExample();
        PatientExample.Criteria criteria=patientExample.createCriteria();
        criteria.andIdEqualTo(id);
        return patientMapper.deleteByExample(patientExample);
    }
}
